﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;

namespace RESP_IIWaterAssessment
{
    public partial class TechBalanceFormReport : Form
    {
        public TechBalanceFormReport()
        {
            InitializeComponent();
        }
        const string catTechState = "TechState", catTechKind = "TechKind";
        int techKind, avpID, regID, techState;

        private void TechBalanceFormReport_Load(object sender, EventArgs e)
        {
            cbRegion.SelectedIndexChanged -= cbRegion_SelectedIndexChanged;
            cbRegion.DataSource = UtilClass.FillCBRegions();
            cbRegion.DisplayMember = "Name";
            cbRegion.ValueMember = "ID";

            if (InformationClass.isAdmin)
            {
                cbRegion.SelectedIndex = -1;
                cbRegion.Text = string.Empty;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
            }
            else
            {
                chbRegion.Checked = true;
                cbRegion.Enabled = chbRegion.Enabled = false;
                regID = InformationClass.regionID;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
                cbRegion.SelectedValue = regID;
                cbRegion_SelectedIndexChanged(cbRegion, null);
                cbAVP.Enabled = true;
                cbAVP.Text = string.Empty;
                //toolStrip1.Visible = false;
            }
            nudReleaseYear.Maximum = nudReleaseYear.Value = DateTime.Now.Year;

            cbCondition.SelectedIndexChanged -= cbCondition_SelectedIndexChanged;
            cbCondition.DataSource = UtilClass.FillCBCategory(catTechState);
            cbCondition.DisplayMember = "Name";
            cbCondition.ValueMember = "ID";
            cbCondition.SelectedIndex = -1;
            cbCondition.Text = string.Empty;
            cbCondition.SelectedIndexChanged += new EventHandler(cbCondition_SelectedIndexChanged);

            cbTechKind.SelectedIndexChanged -= cbTechKind_SelectedIndexChanged;
            cbTechKind.DataSource = UtilClass.FillCBCategory(catTechKind);
            cbTechKind.DisplayMember = "Name";
            cbTechKind.ValueMember = "ID";
            cbTechKind.SelectedIndex = -1;
            cbTechKind.Text = string.Empty;
            cbTechKind.SelectedIndexChanged += new EventHandler(cbTechKind_SelectedIndexChanged);
        }

        private void cbRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbRegion.SelectedIndex == -1)
                return;

            regID = Convert.ToInt32(cbRegion.SelectedValue);
            cbAVP.SelectedIndexChanged -= cbAVP_SelectedIndexChanged;
            cbAVP.DataSource = UtilClass.FillAVP(Convert.ToInt32(cbRegion.SelectedValue));
            cbAVP.ValueMember = "ID";
            cbAVP.DisplayMember = "Name";
            cbAVP.SelectedIndex = -1;
            cbAVP.Text = string.Empty;
            cbAVP.SelectedIndexChanged += new EventHandler(cbAVP_SelectedIndexChanged);
            cbAVP.Enabled = true;
        }

        private void cbAVP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbAVP.SelectedIndex >= 0)
            {
                avpID = Convert.ToInt32(cbAVP.SelectedValue);
            }
            else
                avpID = 0;
        }

        private void cbTechKind_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbTechKind.SelectedIndex >= 0)
            {
                techKind = Convert.ToInt32(cbTechKind.SelectedValue);
            }
            else
                techKind = 0;
        }

        private void cbCondition_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbCondition.SelectedIndex >= 0)
            {
                techState = Convert.ToInt32(cbCondition.SelectedValue);
            }
            else
                techState = 0;
        }

        private void buttonShow_Click(object sender, EventArgs e)
        {
            int? rId, aId, tk, ts, year;

            if (chbRegion.Checked && regID > 0)
                rId = regID;
            else
                rId = null;

            if (chbAVP.Checked && avpID > 0)
                aId = avpID;
            else
                aId = null;

            if (chbTechKind.Checked && techKind > 0)
                tk = techKind;
            else
                tk = null;

            if (chbCondition.Checked && techState > 0)
                ts = techState;
            else
                ts = null;

            if (chbYear.Checked)
                year = (int)nudReleaseYear.Value;
            else
                year = null;

            dgvTechState.DataSource = DBClass.RESPContext.TechBalance(rId, aId, tk, ts, year);

            if (InformationClass.Lang)
            {
                dgvTechState.Columns["Name"].HeaderText = "АВП";
                dgvTechState.Columns["techkind"].HeaderText = "Тип Техники";
                dgvTechState.Columns["TechName"].HeaderText = "Название Техники";
                dgvTechState.Columns["releaseyear"].HeaderText = "Год выпуска";
                dgvTechState.Columns["cost"].HeaderText = "Стоимость";
                dgvTechState.Columns["condition"].HeaderText = "Состояние";
            }
            else
            {
                dgvTechState.Columns["Name"].HeaderText = "СИУ номи";
                dgvTechState.Columns["techkind"].HeaderText = "Техника Тури";
                dgvTechState.Columns["TechName"].HeaderText = "Техника номи";
                dgvTechState.Columns["releaseyear"].HeaderText = "Ишлаб чиқарилган йили";
                dgvTechState.Columns["cost"].HeaderText = "Нарҳи";
                dgvTechState.Columns["condition"].HeaderText = "Ҳолати";
            }
        }

        private void dgvTechState_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex.Equals(dgvTechState.Columns["techkind"].Index) && e.Value != null)
            {
                e.Value = UtilClass.GetCategoryDisplayName((int)e.Value, catTechKind);
            }
            if (e.ColumnIndex.Equals(dgvTechState.Columns["condition"].Index) && e.Value != null)
            {
                e.Value = UtilClass.GetCategoryDisplayName((int)e.Value, catTechState);
            }
            if (e.ColumnIndex.Equals(dgvTechState.Columns["cost"].Index) && e.Value != null)
            {
                e.Value = ((decimal)e.Value).ToString("N");
            }
        }

        Sheets excelsheets;
        Worksheet excelworksheet;
        Microsoft.Office.Interop.Excel.Application excelapp;
        Workbook excelappworkbook;
        Workbooks excelappworkbooks;

        private void buttonExcel_Click(object sender, EventArgs e)
        {
            if (dgvTechState.RowCount > 0)
            {
                MessageBox.Show(msgClass.msgWaiting);
                GetReport();
                excelworksheet.Select(1);
                excelapp.Visible = true;
            }
        }

        private void GetReport()
        {
            excelapp = new Microsoft.Office.Interop.Excel.Application();
            excelapp.SheetsInNewWorkbook = 1;

            if (InformationClass.Lang)
                excelapp.Workbooks.Open(System.Windows.Forms.Application.StartupPath + @"\Templates\i7r9.xls", false, true, Type.Missing, Type.Missing, Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            else
                excelapp.Workbooks.Open(System.Windows.Forms.Application.StartupPath + @"\Templates\i7r9uz.xls", false, true, Type.Missing, Type.Missing, Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            excelappworkbook = excelapp.Workbooks[1];
            excelsheets = excelappworkbook.Worksheets;// .Worksheets[0]; //Sheets 
            excelworksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelsheets.get_Item(1);

            excelworksheet.Cells.NumberFormat = "@";

            if (chbRegion.Checked)
                excelworksheet.Cells[4, 3] = cbRegion.Text;
            if (chbYear.Checked)
                excelworksheet.Cells[5, 3] = nudReleaseYear.Value.ToString();

            for (int i = 0; i < dgvTechState.Rows.Count; i++)
            {
                for (int j = 0; j < dgvTechState.Columns.Count; j++)
                {
                    excelworksheet.Cells[i + 8, dgvTechState.Columns[j].DisplayIndex + 3] = dgvTechState[j, i].FormattedValue;
                    addBorder(excelworksheet.Cells[i + 8, dgvTechState.Columns[j].DisplayIndex + 3]);
                }
                //excelworksheet.Cells[i + 8, 3] = UtilClass.GetRegionName((string)dgvTechState["Name", i].Value);
                excelworksheet.Cells[i + 8, 2] = i + 1;
                addBorder(excelworksheet.Cells[i + 8, 2]);
            }
            excelworksheet.Columns.AutoFit();
            excelworksheet.Rows.AutoFit();
        }

        private void addBorder(object p)
        {
            Range tmpRange = excelworksheet.get_Range(p, p);
            tmpRange.Borders[XlBordersIndex.xlEdgeLeft].LineStyle = XlLineStyle.xlContinuous;
            tmpRange.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
            tmpRange.Borders[XlBordersIndex.xlEdgeRight].LineStyle = XlLineStyle.xlContinuous;
            tmpRange.Borders[XlBordersIndex.xlEdgeTop].LineStyle = XlLineStyle.xlContinuous;
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            if (dgvTechState.RowCount > 0)
                PrintDGV.Print_DataGridView(dgvTechState);
        }
    }
}
